package com.example.newbilityblog.blog;

import com.example.newbilityblog.blog.api.response.BlogResponse;
import com.example.newbilityblog.blog.service.BlogRepresentationService;
import com.example.newbilityblog.blog.service.ConfigService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    private final BlogRepresentationService service;
    private final ConfigService configService;

    public IndexController(BlogRepresentationService service, ConfigService configService) {
        this.service = service;
        this.configService = configService;
    }

    @GetMapping({"/", "/index", "index.html"})
    public String index(HttpServletRequest request, Pageable pageable) {
        var blogs = service.getBlogs(pageable);
        request.setAttribute("blogPageResult", blogs);
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/amaze/index";
    }

    @GetMapping("/blog/{blogId}")
    public String detail(HttpServletRequest request, @PathVariable("blogId") Long blogId) {
        var blogDetail = service.getBlog(blogId);

        request.setAttribute("blogDetail", blogDetail);
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/amaze/detail";
    }


    @GetMapping("/page/{pageId}")
    public String page(
            HttpServletRequest request,
//            @PageableDefault(
//            page = 0, value = 2, sort = {"createdDate"}, direction = Sort.Direction.DESC) Pageable pageable,
            @PathVariable("pageId") int pageId) {
        var pageable = PageRequest.of(pageId-1, 2);
        var blogs = service.getBlogs(pageable);
        request.setAttribute("blogPageResult", blogs);
        request.setAttribute("configurations", configService.getAllConfigs());
        return "blog/amaze/index";
    }
}
